﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SDService;
using SDService.Model;

namespace SDService.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class VersionManagesController : ControllerBase
    {
        private readonly SDDBContext _context;

        public VersionManagesController(SDDBContext context)
        {
            _context = context;
        }

        // GET: api/VersionManages
        [HttpGet]
        public List<VersionManage> GetVersionManage()
        {
            return _context.VersionManage.ToList();
        }

        // GET: api/VersionManages/5
        [HttpGet("{appCode}")]
        public List<VersionManage> GetVersionManage([FromRoute] int appCode)
        {
           return  _context.VersionManage.Where(o => o.AppCode == appCode).Select(o=>new VersionManage { AppCode=o.AppCode, Id= o.Id, Name=o.Name, Num=o.Num }).ToList();
            
            //var linq= from v in _context.VersionManage where v.AppCode == appCode select v;
            //return linq?.ToList();
        }

        // GET: api/VersionManages/5
        [HttpGet("{appCode}/{num}")]
        public VersionManage GetVersionManage([FromRoute] int appCode, [FromRoute] string num)
        {
            if (appCode < 1)
                appCode = 3;
            var linq = from v in _context.VersionManage orderby v.Num descending where v.AppCode == appCode select v;
            if (linq == null)
                return null;

            if (num.ToLower() == "max")
                return linq.First();
            else
                return linq.FirstOrDefault(v=>v.Num==num);
        }

        // PUT: api/VersionManages/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutVersionManage([FromRoute] int id, [FromBody] VersionManage versionManage)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != versionManage.Id)
            {
                return BadRequest();
            }

            _context.Entry(versionManage).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!VersionManageExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/VersionManages
        [HttpPost]
        public async Task<IActionResult> PostVersionManage([FromBody] VersionManage versionManage)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            _context.VersionManage.Add(versionManage);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetVersionManage", new { id = versionManage.Id }, versionManage);
        }

        // DELETE: api/VersionManages/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteVersionManage([FromRoute] string id)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var versionManage = await _context.VersionManage.FindAsync(id);
            if (versionManage == null)
            {
                return NotFound();
            }

            _context.VersionManage.Remove(versionManage);
            await _context.SaveChangesAsync();

            return Ok(versionManage);
        }

        private bool VersionManageExists(int id)
        {
            return _context.VersionManage.Any(e => e.Id == id);
        }
    }
}